import { useIpcRenderer } from "../utils/ipcRenderer";
import * as PathUtils from "../utils/pathUtils";
import type { Setting } from "@model/setting";
import { defineStore } from "pinia";

const UseSetting = defineStore("setting", {
  state() {
    const setting = PathUtils.settingInit;
    return {
      setting: setting,
    };
  },
  actions: {
    setSetting: function (setting: Setting) {
      this.setting.setInfo(setting);
      // this.pullGitProject(setting.git)
    },
    getSetting: function (): Setting {
      return this.setting;
    },
    freshSetting: async function () {
      const setting: Setting = await useIpcRenderer().invoke("fresh-setting");
      this.setSetting(setting);
    },
    chooseFile: async function (pathStr: string) {
      const path = await useIpcRenderer().invoke("choose-file-path", this.getSetting().dataPath, pathStr);
      return path;
    },
    chooseDir: async function (dirName: string) {
      const path = await useIpcRenderer().invoke("choose-dir-path", dirName);
      return path;
    },
    saveSetting: async function () {
      await useIpcRenderer().invoke("save-setting", JSON.stringify(this.getSetting()));
    },
    fresh: async function <T>(pathStr: string, t: T) {
      const contentStr = await useIpcRenderer().invoke("fresh", this.getSetting().dataPath, pathStr);
      t = JSON.parse(contentStr);
      return t;
    },
    save2Local: async function (originalPtah: string, dir: string) {
      // console.log(content)
      //console.log(JSON.stringify(content))
      await useIpcRenderer().invoke("save-to-local", originalPtah, dir);
      this.addGitProject();
    },
    save2File: async function (pathStr: string, content: object) {
      // console.log(content)
      //console.log(JSON.stringify(content))
      await useIpcRenderer().invoke("save-to-file", this.getSetting().dataPath, pathStr, JSON.stringify(content));
      this.addGitProject();
    },
    getFileList: async function (pathStr: string) {
      // console.log(content)
      // console.log(JSON.stringify(content))
      return await useIpcRenderer().invoke("get-file-list", this.getSetting().dataPath, pathStr);
    },
    getFileFullPath: async function (pathStr: Array<string>, paths: string) {
      // console.log(content)
      // console.log(JSON.stringify(content))
      return await useIpcRenderer().invoke(
        "get-file-full-path",
        this.getSetting().dataPath,
        JSON.stringify(pathStr),
        paths,
      );
    },
    getFileBasePath: async function (pathStr: Array<string>) {
      // console.log(content)
      // console.log(JSON.stringify(content))
      return await useIpcRenderer().invoke("get-file-base-path", JSON.stringify(pathStr));
    },
    deleteFileList: async function (pathStr: Array<string>) {
      // console.log(content)
      // console.log(JSON.stringify(content))
      return await useIpcRenderer().invoke("delete-file-list", JSON.stringify(pathStr));
    },
    getCacheModel: async function <T>(pathStr: string, key: string, map: Map<string, T>, newT: T) {
      let model = map.get(key);
      //console.log("getTableData",model,map)
      if (model === undefined) {
        // const setting = this.getSetting();
        //console.log("getTableData",setting.dataPath,pathStr)
        //console.log("getTableData",setting,pathStr)
        const contentStr = await useIpcRenderer().invoke("fresh", this.getSetting().dataPath, pathStr);
        //console.log("getTableData",contentStr)
        if (contentStr !== undefined && contentStr.length > 0) {
          model = JSON.parse(contentStr);
        } else model = newT;
        if (model === undefined) model = newT;
        //console.log("getCacheModel",contentStr,map,pathStr)
        map.set(key, model);
      }
      return model;
    },
    pullGitProject: async function (gitProject: string) {
      // console.log(content)
      // console.log(JSON.stringify(content))
      return await useIpcRenderer().invoke("pull-git-project", gitProject, this.getSetting().dataPath);
    },
    addGitProject: function () {
      // console.log(content)
      // console.log(JSON.stringify(content))
      return useIpcRenderer().invoke("add-git-project", this.getSetting().git, this.getSetting().dataPath);
    },
    commitGitProject: function () {
      // console.log(content)
      // console.log(JSON.stringify(content))
      return useIpcRenderer().invoke("commit-git-project", this.getSetting().git, this.getSetting().dataPath);
    },
  },
  getters: {},
});
export default UseSetting;
